<?php

class CalculatorController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {

        //Initialize
        if ($this->_request->isPost()) {

            $postedData = $this->_request->getPost();

            //Initialize Calculator data
            $calculatorData = Array();
            $calculatorData['cal_name'] = $postedData['cal_name'];
            $calculatorData['cal_costprice'] = GTL_Common::setNumberFormat($postedData['cal_costprice']);
            $calculatorData['cal_sellingprice'] = GTL_Common::setNumberFormat($postedData['cal_sellingprice']);
            $calculatorData['cal_created'] = date('Y-m-d H:i:s');
            $calculatorData['cal_profit_loss_margin'] = GTL_Common::setNumberFormat($postedData['cal_profit_loss_margin']);


            //Load Quotation Model
            $calculatorModel = new Model_Calculator();
            $cal_id = $calculatorModel->save($calculatorData, 'cal_id');

            //Load Quotation Reference Table
            $calculatorRefModel = new Model_Calculatorreference();

            //Initialize Quotation Reference Data
            $calr_typeData = $postedData['type'];
            $calr_ref_idData = $postedData['inventorylist'];
            $calr_qtyData = $postedData['qty'];
            $calr_materialcostData = $postedData['cost'];
            $calr_materialsrpData = $postedData['srp'];
            $calr_uomData = $postedData['uom'];
            $calr_nameData = $postedData['product_material_code'];
            $calr_totalmaterialcostData = $postedData['totalcost'];
            $calr_totalmaterialsrpData = $postedData['totalsrp'];
            

            $i = 0;
            foreach ($calr_typeData as $type) {
                $calculatorData = Array();
                $calculatorData['calr_type'] = $type;
                $calculatorData['cal_id'] = $cal_id;
                $calculatorData['calr_ref_id'] = $calr_ref_idData[$i];
                $calculatorData['calr_name'] = $calr_nameData[$i];
                $calculatorData['calr_qty'] = $calr_qtyData[$i];
                $calculatorData['calr_uom'] = $calr_uomData[$i];
                $calculatorData['calr_materialcost'] = GTL_Common::setNumberFormat($calr_materialcostData[$i]);
                $calculatorData['calr_materialsrp'] = GTL_Common::setNumberFormat($calr_materialsrpData[$i]);
                $calculatorData['calr_totalmaterialcost'] = GTL_Common::setNumberFormat($calr_totalmaterialcostData[$i]);
                $calculatorData['calr_totalmaterialsrp'] = GTL_Common::setNumberFormat($calr_totalmaterialsrpData[$i]);
                $calculatorRefModel->save($calculatorData, 'calr_id');
                $i++;
            }

            $successmessage = "Calculator generated successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('form', 'quotation', '');
        }
    }

    //Function to give the select list of the products and jobs
    public function generateinventorylistAction() {
        $listString = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['type'] == 'job') {
                $jobModel = new Model_Job();
                $jobArray = $jobModel->fetchJobs("job_status = 'active'");
                if (count($jobArray) > 0) {
                    $listString = "<option value=''>-- Select Job --</option>";
                    foreach ($jobArray as $_job) {
                        $listString .= "<option value='" . $_job['job_id'] . "'>" . $_job['job_code'] . "</option>";
                    }
                }
            } else {
                $productModel = new Model_Product();
                $productArray = $productModel->fetchProducts("prd_status = 'active'");
                if (count($productArray) > 0) {
                    $listString = "<option value=''>-- Select product --</option>";
                    foreach ($productArray as $_product) {
                        $listString .= "<option value='" . $_product['prd_id'] . "'>" . $_product['prd_code'] . "</option>";
                    }
                }
            }
        }
        echo $listString;
        exit;
    }

    //Function to give the data of individual products and job
    public function generateproductitemAction() {
        $returnArray = Array(
            'COST_PRICE' => '0.00',
            'SELLING_PRICE' => '0.00'
        );
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['type'] == 'job') {
                $jobModel = new Model_Job();
                $jobArray = $jobModel->fetchEntryById($postedData['id']);
                if (count($jobArray) > 0) {
                    $costprice = $jobArray['job_materialcost'];
                    $sellingPrice = $jobArray['job_materialsrp'];
                }
            } else {
                $productModel = new Model_Product();
                $productArray = $productModel->fetchEntryById($postedData['id']);
                if (count($productArray) > 0) {
                    $costprice = $productArray['prd_materialcost'];
                    $sellingPrice = $productArray['prd_materialsrp'];
                }
            }
        }
        $returnArray['COST_PRICE'] = $costprice;
        $returnArray['SELLING_PRICE'] = $sellingPrice;
        echo Zend_Json_Encoder::encode($returnArray);
        exit;
    }

    //Function to give the data of individual products and job
    public function generateproductsearchAction() {
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['type'] == 'job') {
                $job = Array(
                    'id' => '',
                    'code' => 'No such job available',
                    'uom' => '',
                    'costprice' => '0.00',
                    'srpprice' => '0.00'
                );
                $jobModel = new Model_Job();
                $jobArray = $jobModel->fetchJobs("`job_code` LIKE '%" . $postedData['search'] . "%'");
                $jsonArray = Array();
                if (count($jobArray) > 0) {
                    foreach ($jobArray as $_job) {
                        $job = Array(
                            'id' => $_job['job_id'],
                            'code' => $_job['job_code'],
                            'uom' => $_job['job_uom'],
                            'costprice' => $_job['job_materialcost'],
                            'srpprice' => $_job['job_materialsrp'],
                        );
                        $jsonArray[] = $job;
                    }
                } else {
                    $jsonArray[] = $job;
                }
                echo Zend_Json_Encoder::encode($jsonArray);
            } else {
                $product = Array(
                    'id' => '',
                    'code' => 'No such product available',
                    'uom' => '',
                    'costprice' => '0.00',
                    'srpprice' => '0.00'
                );
                $productModel = new Model_Product();
                $productArray = $productModel->fetchproducts("`prd_code` LIKE '%" . $postedData['search'] . "%'");
                $jsonArray = Array();
                if (count($productArray) > 0) {
                    foreach ($productArray as $_product) {
                        $product = Array(
                            'id' => $_product['prd_id'],
                            'code' => $_product['prd_code'],
                            'uom' => $_product['prd_uom'],
                            'costprice' => $_product['prd_materialcost'],
                            'srpprice' => $_product['prd_materialsrp'],
                        );
                        $jsonArray[] = $product;
                    }
                } else {
                    $jsonArray[] = $product;
                }
                echo Zend_Json_Encoder::encode($jsonArray);
            }
        }
        exit;
    }

    //Function to give the data of individual calculator
    public function generatecalculatosearchAction() {
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            $calculator = Array(
                        'id' => '',
                        'name' => "No such calculator available",
                        'costprice' => '0.00',
                        'srpprice' => '0.00',
                        'CALCULATOR_REFERENCE' => Array(),
                    );
            $calculatorModel = new Model_Calculator();
            $calArray = $calculatorModel->fetchCalculators("`cal_name` LIKE '%" . $postedData['search'] . "%'");
            $jsonArray = Array();
            if (count($calArray) > 0) {
                foreach ($calArray as $key => $_calc) {
                    $calculator = Array(
                        'id' => $key,
                        'name' => $_calc['CALCULATOR']['cal_name'],
                        'costprice' => $_calc['CALCULATOR']['cal_costprice'],
                        'srpprice' => $_calc['CALCULATOR']['cal_sellingprice'],
                        'CALCULATOR_REFERENCE' => $_calc['CALCULATOR_REFERENCE'],
                    );
                    $jsonArray[] = $calculator;
                }
            } else {
                $jsonArray[] = $calculator;
            }
            echo Zend_Json_Encoder::encode($jsonArray);
        }
        exit;
    }

}

?>
